package models

import (
	"time"
	"github.com/astaxie/beego/orm"
	"hw_admin/helper"
)

//后台 模块
type AdminModule struct {
	Id 		int64 `orm:"column(id)"`
	Name 	string `orm:"column(name)"`
	Url		string `orm:"column(url)"`
	ParentId int64	`orm:"column(parentId)"` //oneToOne
	ModuleImage	string `orm:"column(moduleImage)"`
	Description	string `orm:"column(description)"`
	Sort	uint8 `orm:"column(sort)"`
	IsHide	uint8 `orm:"column(isHide)"`
	UserId 	int64 `orm:"column(userId)"`
	CreateTime time.Time `orm:"column(createTime)"`
	Ico string `orm:"column(ico)"`
}

//二级菜单
type VModule struct {
	Id          int64
	Name        string
	Url         string
	ParentId    int64
	ModuleImage string
	Description string
	Sort        uint8
	IsHide      uint8
	UserId      int64
	CreateTime  time.Time
	Ico string
	Child []AdminModule
}


//角色
type AdminRole struct {
	Id int64 `orm:"column(id)"`
	Name string `orm:"column(name)"`
	Number uint16 `orm:"column(number)"`
	Description string `orm:"column(description)"`
	UserId uint16 `orm:"column(userId)"`
	CreateTime time.Time `orm:"column(createTime)"`
}

//授权 分配表
type AdminRolemodule struct {
	Id int64
	RoleId int64  //一对一
	ModuleId int64  //一对一
}

//系统配置键表
type AdminSystemconfig struct {
	Id int64
	DisplayName string
	ConfigKey string
	ConfigValue string
	IsReadOnly	uint8
	Description string
	UserId int64
	CreateTime time.Time
}

//后台用户
type AdminUser struct {
	Id int64
	UserAccount string
	Password string
	UserImage string
	JobPosition string
	RoleId int64
	RealName string
	Sex uint8
	Address string
	AboutMe string
	Telephone string
	CreateTime time.Time
	OptUserId int64
	Isenable uint8
}

type AdminUserRole struct {
	Id int64
	UserId int64
	RoleId int64
}

type Int64 int64

//用户登录
//@param userAccount 账号
//@param password 密码
func (*AdminUser) Login(userAccount,password string) (AdminUser,error) {
	user := AdminUser{}
	pazzword := helper.MD5(password + helper.ENCRYPT_SECRET_KEY)
	err := orm.NewOrm().QueryTable("admin_user").Filter("user_account", userAccount).Filter("password", pazzword).One(&user)
	if err != nil {
		return user,err
	}
	return user,err
}

//通过ID获取用户信息
//@param id 主键ID
func (this *AdminUser) GetById (id int64){
	orm.NewOrm().QueryTable("admin_user").Filter("id",id).One(this)
}


//获取菜单
//
func (*AdminUser) GetModels(roleId int64) ([]VModule,error) {
	var res []AdminModule
	_,err := orm.NewOrm().Raw("SELECT m.id,m.name,m.url,m.parentId,m.moduleImage ,m.description,m.sort,m.isHide,m.userId,m.createTime,m.ico FROM admin_rolemodule r LEFT JOIN admin_module m ON r.moduleId = m.id WHERE m.parentId = 0 and r.roleId = ?",roleId).QueryRows(&res)

	result := make([]VModule,len(res))
	for i,v := range res {
		var child []AdminModule
		_,err1 := orm.NewOrm().Raw("SELECT m.id,m.name,m.url,m.parentId,m.moduleImage ,m.description,m.sort,m.isHide,m.userId,m.createTime FROM admin_module m WHERE m.parentId = ?",v.Id).QueryRows(&child)
		if err1 == nil {
			var vm = VModule{v.Id,v.Name,v.Url,v.ParentId,v.ModuleImage,v.Description,v.Sort,v.IsHide,v.UserId,v.CreateTime,v.Ico,child}
			result[i] = vm
		}

	}
	return result,err
}


//获取角色
func (AdminRole) GetRole()([]AdminRole,error){
	var roles []AdminRole
	_,err := orm.NewOrm().Raw("SELECT * FROM admin_role ").QueryRows(&roles)
	if err != nil {
		return nil,err
	}
	return roles,err
}